package com.youku.adSDK.Utils
{
   import flash.display.Sprite;
   import flash.utils.*;
   import flash.events.Event;
   import flash.events.TimerEvent;
   import flash.media.SoundTransform;
   import flash.geom.ColorTransform;
   import flash.display.MovieClip;
   import flash.display.DisplayObject;
   
   public class AdTweener extends Object
   {
      
      public function AdTweener(param1:Object, param2:Number, param3:Object) {
         super();
         if(param1 == null)
         {
            return;
         }
         if(!(param3.overwrite == false) && !(param1 == null) || _all[param1] == undefined)
         {
            delete _all[param1];
            _all[param1] = new Dictionary();
         }
         _all[param1][this] = this;
         this.vars = param3;
         this.duration = param2;
         this.delay = (param3.delay) || (0);
         if(param2 == 0)
         {
            this.duration = 0.001;
            if(this.delay == 0)
            {
               this.vars.runBackwards = true;
            }
         }
         this.target = this._endTarget = param1;
         this.onComplete = param3.onComplete;
         this.onCompleteParams = param3.onCompleteParams || [];
         this.onUpdate = param3.onUpdate;
         this.onUpdateParams = param3.onUpdateParams || [];
         this.onStart = param3.onStart;
         this.onStartParams = param3.onStartParams || [];
         if(this.vars.ease == undefined)
         {
            this.vars.ease = easeOut;
         }
         else if(!(this.vars.ease is Function))
         {
            trace("ERROR: You cannot use \'" + this.vars.ease + "\' for the Tweener ease property. Only functions are accepted.");
         }
         
         if(!isNaN(Number(this.vars.autoAlpha)))
         {
            this.vars.alpha = Number(this.vars.autoAlpha);
         }
         else if(!isNaN(Number(this.vars._autoAlpha)))
         {
            this.vars.alpha = this.vars.autoAlpha = Number(this.vars._autoAlpha);
         }
         
         this.tweens = {};
         this.extraTweens = {};
         this.initTime = getTimer();
         if(this.vars.runBackwards == true)
         {
            this.initTweenVals();
         }
         this._active = false;
         if(param2 == 0 && this.delay == 0)
         {
            if(this.vars.autoAlpha == 0)
            {
               this.target.visible = false;
            }
            if(this.onComplete != null)
            {
               this.onComplete.apply(null,this.onCompleteParams);
            }
            removeTween(this);
         }
         else if(!_listening)
         {
            _sprite.addEventListener(Event.ENTER_FRAME,executeAll);
            _timer.addEventListener("timer",killGarbage);
            _timer.start();
            _listening = true;
         }
         
      }
      
      public static var version:Number = 5.6;
      
      public static var killDelayedCallsTo:Function = killTweensOf;
      
      private static var _sprite:Sprite = new Sprite();
      
      private static var _listening:Boolean;
      
      private static var _all:Dictionary = new Dictionary();
      
      private static var _timer:Timer = new Timer(2000);
      
      public static function to(param1:Object, param2:Number, param3:Object) : AdTweener {
         return new AdTweener(param1,param2,param3);
      }
      
      public static function from(param1:Object, param2:Number, param3:Object) : AdTweener {
         param3.runBackwards = true;
         return new AdTweener(param1,param2,param3);
      }
      
      public static function delayedCall(param1:Number, param2:Function, param3:Array = null) : AdTweener {
         return new AdTweener(param2,0,
            {
               "delay":param1,
               "onComplete":param2,
               "onCompleteParams":param3,
               "overwrite":false
            });
      }
      
      public static function removeTween(param1:AdTweener = null) : void {
         if(!(param1 == null) && !(_all[param1.endTarget] == undefined))
         {
            delete _all[param1.endTarget][param1];
         }
      }
      
      public static function killTweensOf(param1:Object = null) : void {
         if(!(param1 == null) && !(_all[param1] == undefined))
         {
            delete _all[param1];
         }
      }
      
      public static function executeAll(param1:Event) : void {
         var _loc4_:Object = null;
         var _loc5_:Object = null;
         var _loc6_:Object = null;
         var _loc2_:Object = _all;
         var _loc3_:int = getTimer();
         for(_loc4_ in _loc2_)
         {
            for(_loc5_ in _loc2_[_loc4_])
            {
               _loc6_ = _loc2_[_loc4_][_loc5_];
               if(!(_loc6_ == null) && (_loc6_.active))
               {
                  _loc6_.render(_loc3_);
               }
            }
         }
      }
      
      public static function killGarbage(param1:TimerEvent) : void {
         var _loc4_:* = false;
         var _loc5_:Object = null;
         var _loc6_:Object = null;
         var _loc7_:Object = null;
         var _loc2_:Object = _all;
         var _loc3_:* = 0;
         for(_loc5_ in _loc2_)
         {
            _loc4_ = false;
            for(_loc6_ in _loc2_[_loc5_])
            {
               _loc4_ = true;
               if(!_loc4_)
               {
                  delete _loc2_[_loc5_];
               }
               else
               {
                  _loc3_++;
               }
            }
         }
         if(_loc3_ == 0)
         {
            _sprite.removeEventListener(Event.ENTER_FRAME,executeAll);
            _timer.removeEventListener("timer",killGarbage);
            _timer.stop();
            _listening = false;
         }
      }
      
      protected static function easeOut(param1:Number, param2:Number, param3:Number, param4:Number) : Number {
         return -param3 * (param1 = param1 / param4) * (param1 - 2) + param2;
      }
      
      private var _sound:SoundTransform;
      
      protected var _endTarget:Object;
      
      protected var _active:Boolean;
      
      protected var _color:ColorTransform;
      
      protected var _endColor:ColorTransform;
      
      public var duration:Number;
      
      public var vars:Object;
      
      public var delay:Number;
      
      public var onComplete:Function;
      
      public var onCompleteParams:Array;
      
      public var onUpdate:Function;
      
      public var onUpdateParams:Array;
      
      public var onStart:Function;
      
      public var onStartParams:Array;
      
      public var startTime:uint;
      
      public var initTime:uint;
      
      public var tweens:Object;
      
      public var extraTweens:Object;
      
      public var target:Object;
      
      public function initTweenVals() : void {
         var _loc2_:String = null;
         var _loc3_:* = NaN;
         var _loc4_:Array = null;
         var _loc5_:* = 0;
         var _loc6_:AdTweener = null;
         var _loc7_:AdTweener = null;
         var _loc8_:Object = null;
         var _loc1_:Number = this.delay - (getTimer() - this.initTime) / 1000;
         if(this.target is Array)
         {
            _loc4_ = [];
            for(_loc2_ in this.vars)
            {
               if(this.vars[_loc2_] is Array)
               {
                  _loc4_ = this.vars[_loc2_];
                  break;
               }
            }
            _loc5_ = 0;
            while(_loc5_ < _loc4_.length)
            {
               if(!(this.target[_loc5_] == _loc4_[_loc5_]) && !(this.target[_loc5_] == undefined))
               {
                  this.tweens[_loc5_.toString()] = 
                     {
                        "o":this.target,
                        "s":this.target[_loc5_],
                        "c":_loc4_[_loc5_] - this.target[_loc5_],
                        "e":this.vars.ease
                     };
               }
               _loc5_++;
            }
         }
         else
         {
            for(_loc2_ in this.vars)
            {
               if(_loc2_ == "volume" && this.target is MovieClip)
               {
                  this._sound = this.target.soundTransform;
                  _loc6_ = new AdTweener(this,this.duration,
                     {
                        "volumeProxy":this.vars[_loc2_],
                        "ease":easeOut,
                        "delay":_loc1_,
                        "overwrite":false,
                        "runBackwards":this.vars.runBackwards
                     });
                  _loc6_.endTarget = this.target;
               }
               else if(_loc2_.toLowerCase() == "mccolor" && this.target is DisplayObject)
               {
                  this._color = this.target.transform.colorTransform;
                  this._endColor = new ColorTransform();
                  if(this.vars.alpha != undefined)
                  {
                     this._endColor.alphaMultiplier = this.vars.alpha;
                     delete this.vars.alpha;
                     delete this.tweens.alpha;
                  }
                  else
                  {
                     this._endColor.alphaMultiplier = this.target.alpha;
                  }
                  if(!(this.vars[_loc2_] == null) && !(this.vars[_loc2_] == ""))
                  {
                     this._endColor.color = this.vars[_loc2_];
                  }
                  _loc7_ = new AdTweener(this,this.duration,
                     {
                        "colorProxy":1,
                        "delay":_loc1_,
                        "overwrite":false,
                        "ease":this.vars.ease,
                        "runBackwards":this.vars.runBackwards
                     });
                  _loc7_.endTarget = this.target;
               }
               else if(!(_loc2_ == "delay" || _loc2_ == "ease" || _loc2_ == "overwrite" || _loc2_ == "onComplete" || _loc2_ == "onCompleteParams" || _loc2_ == "runBackwards" || _loc2_ == "onUpdate" || _loc2_ == "onUpdateParams" || _loc2_ == "autoAlpha" || _loc2_ == "_autoAlpha" || _loc2_ == "onStart" || _loc2_ == "onStartParams"))
               {
                  if(this.target.hasOwnProperty(_loc2_))
                  {
                     if(typeof this.vars[_loc2_] == "number")
                     {
                        _loc3_ = this.vars[_loc2_] - this.target[_loc2_];
                     }
                     else
                     {
                        _loc3_ = Number(this.vars[_loc2_]);
                     }
                     this.tweens[_loc2_] = 
                        {
                           "o":this.target,
                           "s":this.target[_loc2_],
                           "c":_loc3_,
                           "e":this.vars.ease
                        };
                  }
                  else
                  {
                     this.extraTweens[_loc2_] = 
                        {
                           "o":this.target,
                           "s":0,
                           "c":0,
                           "e":this.vars.ease,
                           "v":this.vars[_loc2_]
                        };
                  }
               }
               
               
            }
         }
         if(this.vars.runBackwards == true)
         {
            for(_loc2_ in this.tweens)
            {
               _loc8_ = this.tweens[_loc2_];
               _loc8_.s = _loc8_.s + _loc8_.c;
               _loc8_.c = _loc8_.c * -1;
               if(_loc8_.c != 0)
               {
                  _loc8_.o[_loc2_] = _loc8_.e(0,_loc8_.s,_loc8_.c,this.duration);
               }
            }
            if(this.onUpdate != null)
            {
               this.onUpdate.apply(null,this.onUpdateParams);
            }
         }
         if(typeof this.vars.autoAlpha == "number")
         {
            this.target.visible = !(this.vars.runBackwards == true && this.target.alpha == 0);
         }
      }
      
      public function render(param1:int) : void {
         var _loc3_:Object = null;
         var _loc4_:String = null;
         var _loc2_:Number = (param1 - this.startTime) / 1000;
         if(_loc2_ > this.duration)
         {
            _loc2_ = this.duration;
         }
         for(_loc4_ in this.tweens)
         {
            _loc3_ = this.tweens[_loc4_];
            _loc3_.o[_loc4_] = _loc3_.e(_loc2_,_loc3_.s,_loc3_.c,this.duration);
         }
         if(this.onUpdate != null)
         {
            this.onUpdate.apply(null,this.onUpdateParams);
         }
         if(_loc2_ == this.duration)
         {
            if(typeof this.vars.autoAlpha == "number" && this.target.alpha == 0)
            {
               this.target.visible = false;
            }
            if(this.onComplete != null)
            {
               this.onComplete.apply(null,this.onCompleteParams);
            }
            removeTween(this);
         }
      }
      
      public function get active() : Boolean {
         if(this._active)
         {
            return true;
         }
         if((getTimer() - this.initTime) / 1000 > this.delay)
         {
            this._active = true;
            this.startTime = this.initTime + this.delay * 1000;
            if(this.vars.runBackwards != true)
            {
               this.initTweenVals();
            }
            else if(typeof this.vars.autoAlpha == "number")
            {
               this.target.visible = true;
            }
            
            if(this.onStart != null)
            {
               this.onStart.apply(null,this.onStartParams);
            }
            if(this.duration == 0.001)
            {
               this.startTime = this.startTime - 1;
            }
            return true;
         }
         return false;
      }
      
      public function set endTarget(param1:Object) : void {
         delete _all[this._endTarget][this];
         this._endTarget = param1;
         if(_all[param1] == undefined)
         {
            _all[param1] = new Dictionary();
         }
         _all[param1][this] = this;
      }
      
      public function get endTarget() : Object {
         return this._endTarget;
      }
      
      public function set volumeProxy(param1:Number) : void {
         this._sound.volume = param1;
         this.target.soundTransform = this._sound;
      }
      
      public function get volumeProxy() : Number {
         return this._sound.volume;
      }
      
      public function set colorProxy(param1:Number) : void {
         var _loc2_:Number = 1 - param1;
         this.target.transform.colorTransform = new ColorTransform(this._color.redMultiplier * _loc2_ + this._endColor.redMultiplier * param1,this._color.greenMultiplier * _loc2_ + this._endColor.greenMultiplier * param1,this._color.blueMultiplier * _loc2_ + this._endColor.blueMultiplier * param1,this._color.alphaMultiplier * _loc2_ + this._endColor.alphaMultiplier * param1,this._color.redOffset * _loc2_ + this._endColor.redOffset * param1,this._color.greenOffset * _loc2_ + this._endColor.greenOffset * param1,this._color.blueOffset * _loc2_ + this._endColor.blueOffset * param1,this._color.alphaOffset * _loc2_ + this._endColor.alphaOffset * param1);
      }
      
      public function get colorProxy() : Number {
         return 0;
      }
   }
}
